/*
 * CS 567 GUI - Semester Project - "DecisionLists"
 * Jason Frank
 * Spring 2011
 *
 * This File:
 *
 */

package Decision_Lists;

import com.thebuzzmedia.imgscalr.Scalr;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 */
public class GridView extends JPanel implements DL_ViewInterface {

  String viewName = "Grid";
  DL_Model model;
  
  int imageWidth = 375;

  Vector<GridPanel> gridPanels = new Vector<GridPanel>();

  //////////////////////////////
  //// NESTED CLASS GridPanel //
  public class GridPanel extends JPanel {

    List_Item_Data item;
    JLabel title = new JLabel();
    JLabel imageIconLabel = new JLabel();
    ImageResizeToPanel imagePanel;

    // Constructor
    public GridPanel(List_Item_Data item) {

      this.item = item;
      title.setText(item.getName());

      imagePanel = new ImageResizeToPanel(item.getMainPicBufferedImage(), imageWidth);
      //imagePanel = new ImageResizeToPanel(item.getMainPicBufferedImage(), getWidth());

      setBorder(BorderFactory.createEtchedBorder());

      layoutGridPanel();
    } //// end nested class constructor ///

    protected void layoutGridPanel() {
      setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

      //add(title);
      //add(imagePanel);

      Box mainHorizBox = Box.createHorizontalBox();
      Box mainVertBox =  Box.createVerticalBox();

      mainHorizBox.add(Box.createHorizontalGlue());

      mainVertBox.add(Box.createVerticalGlue());
      mainVertBox.add(title);
      mainVertBox.add(Box.createRigidArea(new Dimension(0, 15)));
      mainVertBox.add(imagePanel);
      mainVertBox.add(Box.createVerticalGlue());

      mainHorizBox.add(mainVertBox);

      mainHorizBox.add(Box.createHorizontalGlue());

      add(mainHorizBox);
    }
    
  } //////// END NESTED CLASS GridPanel /////



  // Constructor
  public GridView(DL_Model model)  {

    this.model = model;
    this.model.addObserver(this, viewName, true);

    // Add a new GridPanel to the vec for each list item
    for(List_Item_Data item: model.getItemsVec()) {

      gridPanels.add(new GridPanel(item));
    }

    layoutPanel();

  } ////// end constructor //////



  protected void layoutPanel() {

    setLayout(new GridLayout(2, 2));

    for(GridPanel gridPanel: gridPanels) {
      add(gridPanel);
    }

  } ///////// end layoutPanel() ////////




  /////////////////////////////////////////////////
  ////// Methods Required by the Interface ////////

  // The Model calls this to notify this View of a changed current view
  // This method removes the previous center view and adds the new center view.
  public void curViewChanged(String old_view_name, String new_view_name) {
    
  }

  public void itemChanged() {
    
  }

  public void itemAdded(List_Item_Data newItem) {

    gridPanels.add(new GridPanel(newItem));
    removeAll();
    layoutPanel();
  }

  public String getViewName() {
      return viewName;
  }
  /////// end required by interface functions  //////
  ///////////////////////////////////////////////////

} //////////////////// end class GridView /////////////////////////////
